package com.job;

import com.common.utils.StringUtils;
import com.common.utils.SysConfig;
import com.model.domain.machine.GetMachineInfoDO;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.machine.MachineInfo;
import com.service.device.DeviceObdBaseInfoService;
import com.service.machine.MachineInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-08-28 8:04
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class MachineInfoJob {
    private final MachineInfoService machineInfoService;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    /**
     * 更新农机设备的在线状态
     * @author tm
     * @date 2023-05-27 10:10
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void updateMachineOnLineStatus(){
        if(SysConfig.getJobSwitch()==0)return;
        GetMachineInfoDO getMachineInfoDO = new GetMachineInfoDO();
        getMachineInfoDO.setSortRow("id");
        List<MachineInfo> machineList = machineInfoService.getAllList(getMachineInfoDO);
        if(machineList!=null && machineList.size()>0){
            machineList.forEach(item -> {
                if(StringUtils.isNotBlank(item.getTerminalNum())){
                    DeviceObdBaseInfo baseInfo = deviceObdBaseInfoService.getByDtu(item.getTerminalNum());
                    if(baseInfo!=null && StringUtils.isNotBlank(baseInfo.getLat())){
                        item.setLat(baseInfo.getLat());
                        item.setLon(baseInfo.getLon());
                        item.setDataTime(baseInfo.getDataTime());
                        machineInfoService.update(item);
                    }
                }
            });
        }
    }
}
